import { defineStore } from 'pinia'
import request from '@/utils/sj-request'

export const usetikuUserStore = defineStore('user-tiku', {
  state: () => ({
    token: localStorage.getItem('token') || '',
    userInfo: localStorage.getItem('userInfo') && localStorage.getItem('userInfo') !== 'undefined' ? JSON.parse(localStorage.getItem('userInfo')) : {}
  }),
  
  actions: {
    // 登录
    async login(account, password) {
      try {
        // 这里替换为实际的API接口
        const response = await request({
          url: '/auth/login',
          method: 'post',
          data: {
            account,
            password
          }
        })
        
        // 根据接口返回的数据格式解析token和用户信息，并给account取别名防止覆盖原数据
        const { token, id, account: responseAccount, roleId, roleName, subjectName, subjectId, subjects } = response.data
        // 构建用户信息对象
        const userInfo = { 
          id, 
          account: responseAccount, 
          roleId, 
          roleName, 
          subjectName, // 保留向后兼容
          subjectId,   // 保留向后兼容
          subjects     // 添加学科列表信息
        }
        // 保存token和用户信息
        this.token = token
        this.userInfo = userInfo
        
        localStorage.setItem('token', token)
        localStorage.setItem('userInfo', JSON.stringify(userInfo))
        return Promise.resolve(response.data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 登出
    logout() {
      this.token = ''
      this.userInfo = {}
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
    },
    
    // 重置密码
    async resetPassword(account, email, verificationCode, newPassword) {
      try {
        // 使用封装好的request服务代替axios
        const response = await request({
          url: '/auth/reset-password',
          method: 'post',
          data: {
            account,
            email,
            verificationCode,
            newPassword
          }
        })
        
        // 如果请求成功但业务逻辑失败，也应该抛出错误
        if (response.code && response.code !== 200) {
          return Promise.reject({
            message: response.message || '密码重置失败',
            code: response.code
          })
        }
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject({
          message: error.message || '密码重置失败',
          code: error.code || 500
        })
      }
    },
    
    // 发送验证码
    async sendVerificationCode(account, email) {
      try {
        // 使用封装好的request服务代替axios
        const response = await request({
          url: '/auth/forgot-password',
          method: 'post',
          data: {
            account,
            email
          }
        })
        
        // 如果请求成功但业务逻辑失败，也应该抛出错误
        if (response.code && response.code !== 200) {
          return Promise.reject({ 
            message: response.message || '验证码发送失败',
            code: response.code 
          })
        }
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject({
          message: error.message || '验证码发送失败',
          code: error.code || 500
        })
      }
    }
  }
})